package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
//影响接口文档的生成
//@Api用在类上，例如Controller，表示对类的说明
//@ApiModel用在类上，例如entity、DTO、VO
//@ApiModelProperty用在属性上，描述属性信息
//@ApiOperation用在方法上，例如Controller的方法，说明方法的用途、作用
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    //新增员工
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工:{}", employeeDTO);//{}是占位符,可以把后面的值动态的传递过去
        System.out.println("当前线程的id" + Thread.currentThread().getId());

        employeeService.save(employeeDTO);

        return Result.success();
    }

    //员工分页查询
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询，参数为:{}", employeePageQueryDTO);
        PageResult pageResult=employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    //启用禁用员工账号
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status, Long id) {//路径参数要加注解@PathVariable
        /*@PathVariable注解明确指出了方法参数的值是从URL路径中提取的，而不是从请求体或其他地方获取。
自动绑定：该注解帮助Spring框架自动将URL中的占位符绑定到控制器的方法参数上。
增强可读性：使用@PathVariable可以让代码更加清晰易懂，表明这个参数是与路径相关的变量。
例如，对于路径/users/{id}，如果你想获取{id}部分的值，就可以在对应的处理器方法参数前加上@PathVariable注解来接收这个值。*/
        log.info("启用禁用员工账号:{},{}", status, id);
        employeeService.startOrStop(status, id);
        return Result.success();
    }


    @GetMapping("/{id}")
    //根据id查询员工
    @ApiOperation("根据id查询员工")
    public Result<Employee> getById(@PathVariable Integer id){
        log.info("根据id查询员工：{}",id);
        Employee employee=employeeService.getById(id);
        employee.setPassword("****");//提升安全性
        return Result.success(employee);
    }


    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody  EmployeeDTO employeeDTO){
        log.info("编辑员工信息:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}














